package com.cloudm.technician.ui.modules.personal.change_password

import com.cloudm.technician.data.network.PersonalApi
import com.cloudm.technician.ui.base.RxPresenter

/**
 * 作者: wesley
 * 时间: 2017/12/26
 * 描述: 业务逻辑
 */
class ChangePasswordPresenter(private val view: ChangePasswordContract.View,
                              private val personalApi: PersonalApi) : RxPresenter(),
        ChangePasswordContract.Presenter {


    override fun changePassword(oldPassword: String?, newPassword: String?,
                                confirmPassword: String?) {

        if (oldPassword == null || oldPassword.isNullOrEmpty()) {
            view.showOlePasswordEmpMsg()
            return
        }

        if (newPassword == null || newPassword.isNullOrEmpty()) {
            view.showNewPasswordEmpMsg()
            return
        }

        if (newPassword.length < 6) {
            view.showNewPasswordLengthMsg()
            return
        }

        if (newPassword != confirmPassword) {
            view.showNewConfirmNotNameMsg()
            return
        }

        view.showChangePasswordDialog()
        request(personalApi.modifyPassword(oldPassword, newPassword), view, onSuccess = { _, _ ->
            view.hideChangePasswordDialog()
            view.changePasswordSuccess()
        }, onFailure = { errorMsg ->
            view.hideChangePasswordDialog()
            view.changePasswordFailure(errorMsg)
        })

    }
}